// 轮播插件
(function ($) {
$.fn.carousel = function(obj){
	// 参数
	var Default = {
		Bnum : 1,	// 显示个数
		Bsorter : true,	// 是否开启分页器
		Bbutton : true,	// 是否开启前进按钮
		Btouch : true,	// 是否支持触控
		Broll : true,	// 是否自动滚动
		BrollTime : 2000,	// 自动滚动速度
		Bvelocity : 200,	//滚动速度
		Bmargin : 0	//div的间距
	}

	var setting = $.extend({},Default,obj);

	// 插件div对象
	var car = $(this),	// 最外层div
		carShow = car.find(".carousel-container"),	// div轮播显示区域
		carBox = car.find(".carousel-box"),	// div轮播盒子大小
		carSlide = car.find(".carousel-slide"),	// slide轮播盒子
		carConLeft = car.find(".carousel-left"),
		carConRight = car.find(".carousel-right");

	// 全局变量
	var Bleng = carSlide.length;	// 轮播div总个数
	var Bindex = 0;	// 轮播到第几个div
	var BsorHtml = "";	// 分页器html
	var BSmargin = 100 / Bleng;	// 单个div包括外边距的宽度
	var BSwidth = BSmargin - parseInt(setting.Bmargin)*2;	// 单个div相对于box的宽度
	var BSroll = 100 / setting.Bnum;	// 单个div滚动距离

	// 显示区域、slide大小、外边距设置
	carBox.css({"width":(100/setting.Bnum*Bleng)+"%"});
	carSlide.css({
		"width" : BSwidth+"%",
		"margin" : "0 " + setting.Bmargin + "%"
	});

	// 分页器
	if (setting.Bsorter) {
		BsorHtml += "<ul>";
		for (var i = 0; i < Bleng-setting.Bnum+1; i++) {
			BsorHtml += "<li></li>";
		};
		BsorHtml += "</ul>";

		car.append(BsorHtml);

		sorter(0);

		car.find("li").on("click",function(){
			carBox.animate({"left": -$(this).index()*BSroll+"%"},setting.Bvelocity);
			sorter($(this).index());

			Bindex = $(this).index();
		});
		
		// 鼠标经过清除、离开启动
		car.find("li").on("mouseover",function(){
			clearInterval(rollTime);
		});
		car.find("li").on("mouseout",function(){
			rollTime = setInterval(rollAuto,setting.BrollTime);
		});
	};
	function sorter (index){
		car.find("li").eq(index).addClass("sorActive").siblings().removeClass("sorActive");
		// 从这里开始添加
	};

	// 前进按钮控制
	if (setting.Bbutton) {
		carConLeft.show();
		carConRight.show();

		carConRight.on("click",function(){

			Bindex < Bleng-setting.Bnum+1-1 ? (Bindex++) : (Bindex=0);
			carBox.animate({"left": -Bindex*BSroll+"%"},setting.Bvelocity);

			sorter(Bindex);
		});

		carConLeft.on("click",function(){

			Bindex > 0 ? (Bindex--) : (Bindex=Bleng-setting.Bnum+1-1);
			carBox.animate({"left": -Bindex*BSroll+"%"},setting.Bvelocity);

			sorter(Bindex);
		});
	};


	// 鼠标滑动控制
	if (setting.Btouch) {
		// 鼠标起始位置和box的位置变量
		var initialLoca = 0;
		var initialLeft = 0;

		// 鼠标点击时触发拖动事件
		carBox.on("mousedown",function(event){

			initialLoca = event.clientX;
			initialLeft = parseInt($(this).css("left"));

			$(this).on("mousemove",function(e){
				$(this).css({"left":initialLeft+e.clientX-initialLoca+"px"});
			});
		});
		// 鼠标点击离开时触发判断是否滚动
		carBox.on("mouseup",function(event){
			$(this).off("mousemove");

			if (event.clientX-initialLoca>10){

				Bindex > 0 ? Bindex-- : Bindex=Bleng-setting.Bnum+1-1;
				carBox.animate({"left": -Bindex*BSroll+"%"},setting.Bvelocity);

				sorter(Bindex);
			}else{

				if (initialLoca-event.clientX>10){

					Bindex < Bleng-setting.Bnum+1-1 ? Bindex++ : Bindex=0;
					carBox.animate({"left": -Bindex*BSroll+"%"},setting.Bvelocity);

					sorter(Bindex);
				}else{

					carBox.css({"left": -Bindex*BSroll+"%"});
				};
			};

			initialLoca = event.clientX;
		});
	};

	// 自动滚动控制
	if (setting.Broll){
		var rollTime = setInterval(rollAuto,setting.BrollTime);

		// 鼠标经过清除、离开启动
		carShow.on("mouseover",function(){
			clearInterval(rollTime);
		});
		carShow.on("mouseout",function(){
			rollTime = setInterval(rollAuto,setting.BrollTime);
		});

		carConLeft.on("mouseover",function(){
			clearInterval(rollTime);
		});
		carConLeft.on("mouseout",function(){
			rollTime = setInterval(rollAuto,setting.BrollTime);
		});

		carConRight.on("mouseover",function(){
			clearInterval(rollTime);
		});
		carConRight.on("mouseout",function(){
			rollTime = setInterval(rollAuto,setting.BrollTime);
		});

		// 滚动事件
		function rollAuto(){
			Bindex < Bleng-setting.Bnum ? (Bindex++) : (Bindex=0);
			carBox.animate({"left": -Bindex*BSroll+"%"},setting.Bvelocity);

			sorter(Bindex);
		};
	};

}
})(jQuery);
